﻿using Unity.Burst;
using Unity.Collections;
using Unity.Jobs;

namespace Gmae.FlowField.Burst
{
    [BurstCompile]
    public partial struct FindUnitIndexJob : IJobParallelForBatch
    {
        [ReadOnly]
        public NativeArray<int> CoverGridIndexArray;

        [ReadOnly]
        public NativeArray<int> TotalUnitGridIndexToArray;

        /// <summary>
        /// Output -1表示无
        /// </summary>
        [WriteOnly]
        public NativeArray<int> CoverUnitIndexArray;

        [ReadOnly]
        public int UnitCount;

        public void Execute(int startIndex, int count)
        {
            for (int i = 0; i < count; i++)
            {
                var coverGridIndex = CoverGridIndexArray[startIndex + i];
                if (coverGridIndex < 0)
                {
                    this.CoverUnitIndexArray[startIndex + i] = -1;
                    break;
                }
                
                var isMatch = false;
                for (int j = 0; j < UnitCount && j < TotalUnitGridIndexToArray.Length; j++)
                {
                    if (coverGridIndex == TotalUnitGridIndexToArray[j])
                    {
                        this.CoverUnitIndexArray[startIndex + i] = j;
                        isMatch = true;
                        break;
                    }
                }

                if(!isMatch)
                {
                    this.CoverUnitIndexArray[startIndex + i] = -1;
                }
            }
        }
    }
}
